using System;
using System.Collections.Generic;
using SimpleJson;
using Pomelo.DotNetClient;
using UnityEngine;

public class MyPomelo
{
    private PomeloClient pc;
    private string userId = "";
    private string channelId = "sys_0";
    private string host = "127.0.0.1";
    private int port = 3014;
    public bool isLogin = false;

    public MyPomelo(string uid,string _channelId)
    {
        userId = Encrypt(uid);
        channelId = _channelId;
        Login();
    }
    public MyPomelo(string uid, string _channelId,string _host)
    {
        host = _host;
        userId = Encrypt(uid);
        channelId = _channelId;
        Login();
    }
    public MyPomelo(string uid, string _channelId, string _host,int _port)
    {
        host = _host;
        port = _port;
        userId = Encrypt(uid);
        channelId = _channelId;
        Login();
    }
    private string Encrypt(string uid)
    {
        String encryptKey = "Eikd$#*&2#$lklf1ljk34LKDFa2342343%";
        var _encryptKey = Utils.Encode64(encryptKey);
        return Utils.Encode64(uid + _encryptKey);
    }
    private void Login()
    {
        pc = new PomeloClient();

        pc.NetWorkStateChangedEvent += (state) =>
        {
            Debug.Log(state);
        };


        pc.initClient(host, port, () =>
        {
            pc.connect(null, data =>
            {
                Debug.Log("on data back" + data.ToString());
                JsonObject msg = new JsonObject();
                msg["uid"] = userId;
                pc.request("gate.gateHandler.queryEntry", msg, OnQuery);
            });
        });
    }

    private void OnQuery(JsonObject result)
    {
        if (Convert.ToInt32(result["code"]) == 200)
        {
            pc.disconnect();

            string host = (string)result["host"];
            int port = Convert.ToInt32(result["port"]);
            pc = new PomeloClient();

            pc.NetWorkStateChangedEvent += (state) =>
            {
                Debug.Log(state);
            };

            pc.initClient(host, port, () =>
            {
                //事件监听
                pc.on("onChat", OnChat);
                pc.on("onAdd", OnAdd);
                pc.on("onLeave", OnLeave);
                pc.on("onSysPush", OnSysPush);
                pc.on("disconnect", OnDisconnect);

                pc.connect(null, (data) =>
                {
                    Debug.Log("on connect to connector!");

                    //Login
                    JsonObject msg = new JsonObject();
                    msg["userId"] = userId;
                    msg["rid"] = channelId;

                    pc.request("connector.entryHandler.enter", msg, OnEnter);
                });
            });
        }
    }

    private void OnEnter(JsonObject result)
    {
        isLogin = true;
        Debug.Log("on login "+ channelId + " result:" + result.ToString());
    }

    private void OnDisconnect(JsonObject result)
    {
        isLogin = false;
        Debug.Log("on sockect disconnected!");
    }
    private void OnChat(JsonObject result)
    {
        Debug.Log("on sockect onChat!" + channelId + " result:" + result.ToString());
    }
    private void OnSysPush(JsonObject result)
    {
        Debug.Log("on sockect onSysPush!" + channelId + " result:" + result.ToString());
    }
    private void OnAdd(JsonObject result)
    {
        Debug.Log("on sockect onAdd!" + channelId + " result:" + result.ToString());
    }
    private void OnLeave(JsonObject result)
    {
        Debug.Log("on sockect onLeave!" + channelId + " result:" + result.ToString());
    }
    public void Disconnect()
    {
        pc.disconnect();
    }
    public void sendMsg(string msg,string target = "*")
    {
        if (!isLogin)
        {
            Debug.Log("hasvn't login " + channelId);
            return;
        }
        Debug.Log("sendMsg:"+channelId);

        var msgObj = new JsonObject();
        msgObj.Add("param", msg);
        msgObj.Add("from", userId);
        msgObj.Add("target", target);
        pc.request("chat.chatHandler.send", msgObj,(result) =>{
            Debug.Log("sendMsg:" + channelId + " result:" + result.ToString());
        });
    }
}
